﻿using System.Diagnostics;

namespace UIOC.Observable {

    /// <summary>
    /// 可观察对象
    /// </summary>
    public interface IObservableObject : UIOC.IObject {

        /// <summary>
        /// 当发生改变时
        /// </summary>
        public delegate void ChangedDelegate();

        /// <summary>
        /// 当发生改变时
        /// </summary>
        event ChangedDelegate Changed;

        void ClearChanged();
    }

    /// <summary>
    /// 对象
    /// </summary>
    public interface IObject : UIOC.IObject, IObservableObject {
    }

#if !DISABLE_DEBUGGER_ATTRIBUTE
    [DebuggerStepThrough]
    [DebuggerNonUserCode]
#endif
    public abstract class Object : UIOC.Object, IObject {

        public virtual event IObservableObject.ChangedDelegate Changed;

#if !DISABLE_DEBUGGER_ATTRIBUTE
        [DebuggerHidden]
#endif
        protected virtual void NotifyChanged() {
            Changed?.Invoke();
        }

#if !DISABLE_DEBUGGER_ATTRIBUTE
        [DebuggerHidden]
#endif
        public virtual void ClearChanged() {
            Changed = null;
        }

#if !DISABLE_DEBUGGER_ATTRIBUTE
        [DebuggerHidden]
#endif
        protected override void Allocate() {
            base.Allocate();
        }

#if !DISABLE_DEBUGGER_ATTRIBUTE
        [DebuggerHidden]
#endif
        protected override void Release() {
            Changed = null;
            base.Release();
        }

#if !DISABLE_DEBUGGER_ATTRIBUTE
        [DebuggerHidden]
#endif
        protected override void Dispose() {
            Changed = null;
            base.Dispose();
        }
    }
}
